window.onload = function (){
    let show=document.getElementById("show")
    let searchInput=document.getElementById("searchInput")
    let userName=document.getElementById("userName").innerText
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/showAllMenu?userName="+userName,true)
    xhr.send()
    xhr.onload = function () {
        searchInput.innerHTML="<div><input id=\"name\" placeholder=\"按菜品名搜索\">\n" +
            "    <input id=\"price\" placeholder=\"按价格搜索\">" +
            " 类型：<select id=\"type\" >\n" +
            "        <option></option>\n" +
            "        <option>早餐</option>\n" +
            "        <option>中餐</option>\n" +
            "        <option>午餐</option>\n" +
            "        <option>甜食</option>\n" +
            "        <option>米</option>\n" +
            "        <option>面</option>\n" +
            "    </select>" +
            "<button onclick='search()'>搜索</button>\n"+
            "</div>"
        show.innerHTML=''
        show.innerHTML+='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">菜品名</th>\n' +
            '        <th style="border: solid;">种类</th>\n' +
            '        <th style="border: solid;">价格</th>\n' +
            '      </tr>'
        for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].name + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].type + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].price + "元</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr.responseText)[i].id +"' onclick='revise(this)'>修改</button>"+ "</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr.responseText)[i].id+"'   onclick='del(this)'>下架</button>"+ "</td>" +

                "</tr>"
        }
    }
}
let show=document.getElementById("show")
let searchInput=document.getElementById("searchInput")
let menus=document.getElementById("menus")
menus.onclick = function (){
    let searchInput=document.getElementById("searchInput")
    const xhr = new XMLHttpRequest();
    let userName=document.getElementById("userName").innerText
    xhr.open("get", "http://localhost:8080/showAllMenu?userName="+userName,true)
    xhr.send()
    xhr.onload = function () {
         show.innerHTML=''
        searchInput.innerHTML="<div><input id=\"name\" placeholder=\"按菜品名搜索\">\n" +
            "    <input id=\"price\" placeholder=\"按价格搜索\">\n" +
            "    类型：<select id=\"type\" >\n" +
            "        <option></option>\n" +
            "        <option>早餐</option>\n" +
            "        <option>中餐</option>\n" +
            "        <option>午餐</option>\n" +
            "        <option>甜食</option>\n" +
            "        <option>米</option>\n" +
            "        <option>面</option>\n" +
            "    </select>" +
            "<button onclick='search()'>搜索</button>\n"+
            "</div>"
        show.innerHTML+='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">菜品名</th>\n' +
            '        <th style="border: solid;">种类</th>\n' +
            '        <th style="border: solid;">价格</th>\n' +
            '      </tr>'
        for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].name + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].type + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].price+ "元</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr.responseText)[i].id +"' onclick='revise(this)'>修改</button>"+ "</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr.responseText)[i].id+"'   onclick='del(this)'>下架</button>"+ "</td>" +
                "</tr>"
        }

    }
}
let bills=document.getElementById("bills")
bills.onclick = function (){
    let userName=document.getElementById("userName").innerText
    let searchInput=document.getElementById("searchInput")
    searchInput.innerHTML=''
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/showAllBill?shopUserName="+userName,true)
    xhr.send()
    xhr.onload = function () {
        searchInput.innerHTML+="<span><input id=\"user-name\" type=\"text\" placeholder='按用户名查询'>\n" +
            "        <input id=\"bill-name\" type=\"text\" placeholder='按用菜名查询'>\n" +
            "        <input id=\"bill-price\" type=\"text\" placeholder='按价格查询'>\n" +
            "        类型<select id=\"bill-type\" >\n" +
            "               <option></option>\n" +
            "               <option>早餐</option>\n" +
            "               <option>中餐</option>\n" +
            "               <option>午餐</option>\n" +
            "               <option>甜食</option>\n" +
            "               <option>米</option>\n" +
            "               <option>面</option>\n" +
            "           </select>"+
        "<button onclick='searchBill()'>搜索</button></span>\n"
        show.innerHTML=''
        show.innerHTML+="<tr>\n" +
            "    <th style=\"border: solid;\">用户名</th>\n" +
            "    <th style=\"border: solid;\">菜名</th>\n" +
            "    <th style=\"border: solid;\">价格</th>\n" +
            "    <th style=\"border: solid;\">类型</th>\n" +
            "</tr>"
        let v=JSON.parse(xhr.responseText)
        for (let i=0;i<v.length;i++){
            show.innerHTML+="<tr><td style=\"border: solid;\">"+ v[i].userName +"</td>"+
                "<td style=\"border: solid;\">"+ v[i].name +"</td>"+
                "<td style=\"border: solid;\">"+ v[i].price +"</td>"+
                "<td style=\"border: solid;\">"+ v[i].type +"</td>"+
                "</tr>"
        }
    }
}
let addMenu=document.getElementById("addMenu")

addMenu.onclick = function () {
    let searchInput=document.getElementById("searchInput")
    searchInput.innerHTML=''
    show.innerHTML=''
    show.innerHTML+=
        "菜名：<input name='name'><br>" +
        "价格：<input name='price'><br>" +
        "类型：<select name='type'>" +
        "        <option>早餐</option>" +
        "        <option>中餐</option>" +
        "        <option>午餐</option>" +
        "        <option>甜食</option>" +
        "        <option>米</option>" +
        "        <option>面</option>" +
        "</select><br><button onclick='submit()'>添加</button>"
}
function search(){
    let userName=document.getElementById("userName").innerText
    let name=document.getElementById("name").value
    let p=document.getElementById("price").value
    let type=document.getElementById("type").value
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/searchMenu?merchant_name="+userName+"&name="+name+"&p="+p+"&type="+type,true)
    xhr.send()
    xhr.onload = function () {
        show.innerHTML=''
        show.innerHTML+='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">菜品名</th>\n' +
            '        <th style="border: solid;">种类</th>\n' +
            '        <th style="border: solid;">价格</th>\n' +
            '      </tr>'
        for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].name + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].type + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].price+ "元</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr.responseText)[i].id+"' onclick='revise(this)'>修改</button>"+ "</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr.responseText)[i].id+"'   onclick='del(this)'>下架</button>"+ "</td>" +
                "</tr>"
        }

    }
}
function del(e){
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/del?id="+e.id,false)
    xhr.send()

    let show=document.getElementById("show")
    let userName=document.getElementById("userName").innerText
    const xhr1 = new XMLHttpRequest();
    xhr1.open("get", "http://localhost:8080/showAllMenu?userName="+userName,true)
    xhr1.send()
    xhr1.onload = function () {
        show.innerHTML=''
        show.innerHTML+='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">菜品名</th>\n' +
            '        <th style="border: solid;">种类</th>\n' +
            '        <th style="border: solid;">价格</th>\n' +
            '      </tr>'
        for (var i = 0; i < JSON.parse(xhr1.responseText).length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr1.responseText)[i].name + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr1.responseText)[i].type + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr1.responseText)[i].price + "元</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr1.responseText)[i].id +"' onclick='revise(this)'>修改</button>"+ "</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr1.responseText)[i].id+"'   onclick='del(this)'>下架</button>"+ "</td>" +
                "</tr>"
        }
    }

}
function revise(e){
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/findOne?id="+e.id,true)
    xhr.send()
    xhr.onload = function () {
        let v=JSON.parse(xhr.responseText)
        let show=document.getElementById("show")
        let searchInput=document.getElementById("searchInput")
        searchInput.innerHTML=''
        show.innerHTML=''
        show.innerHTML+=
            "菜名：<input name='name' value='"+ v.name +"'><br>" +
            "价格：<input name='price' value='"+ v.price +"'><br>" +
            "类型：<select name='type' value='"+ v.type +"'>" +
            "        <option value='早餐'>"+v.type+"</option>" +
            "        <option value='早餐'>早餐</option>" +
            "        <option value='中餐'>中餐</option>" +
            "        <option value='午餐'>午餐</option>" +
            "        <option value='甜食'>甜食</option>" +
            "        <option value='米'>米</option>" +
            "        <option value='面'>面</option>" +
            "</select><br><button id='"+ v.id +"' onclick='reset(this)'>修改</button>"
    }
}
function submit(){
    let name=document.getElementsByName("name").item(0).value
    let price=document.getElementsByName("price").item(0).value
    let type=document.getElementsByName("type").item(0).value
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/addMenu?name="+name+"&price="+price+"&type="+type,false)
    xhr.send()
        alert("菜品添加成功")
        let show=document.getElementById("show")
        let searchInput=document.getElementById("searchInput")
        let userName=document.getElementById("userName").innerText
        const xhr1 = new XMLHttpRequest();
        xhr1.open("get", "http://localhost:8080/showAllMenu?userName="+userName,true)
        xhr1.send()
        xhr1.onload = function () {
            searchInput.innerHTML="<div><input id=\"name\" placeholder=\"按菜品名搜索\">\n" +
                "    <input id=\"price\" placeholder=\"按价格搜索\">" +
                " 类型：<select id=\"type\" >\n" +
                "        <option></option>\n" +
                "        <option>早餐</option>\n" +
                "        <option>中餐</option>\n" +
                "        <option>午餐</option>\n" +
                "        <option>甜食</option>\n" +
                "        <option>米</option>\n" +
                "        <option>面</option>\n" +
                "    </select>" +
                "<button onclick='search()'>搜索</button>\n"+
                "</div>"
            show.innerHTML=''
            show.innerHTML+='<tr  style="border: solid;">\n' +
                '        <th style="border: solid;">菜品名</th>\n' +
                '        <th style="border: solid;">种类</th>\n' +
                '        <th style="border: solid;">价格</th>\n' +
                '      </tr>'
            for (var i = 0; i < JSON.parse(xhr1.responseText).length; i++) {
                show.innerHTML += "<tr  style=\"border: solid;\">" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr1.responseText)[i].name + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr1.responseText)[i].type + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr1.responseText)[i].price + "元</td>" +
                    "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr1.responseText)[i].id +"' onclick='revise(this)'>修改</button>"+ "</td>" +
                    "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr1.responseText)[i].id+"'   onclick='del(this)'>下架</button>"+ "</td>" +

                    "</tr>"
            }
        }


}
function reset(e){
    let name=document.getElementsByName("name").item(0).value
    let price=document.getElementsByName("price").item(0).value
    let type=document.getElementsByName("type").item(0).value
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/revise?id="+e.id+"&name="+name+"&price="+price+"&type="+type,false)
    xhr.send()

    let show=document.getElementById("show")
    let userName=document.getElementById("userName").innerText
    const xhr1 = new XMLHttpRequest();
    xhr1.open("get", "http://localhost:8080/showAllMenu?userName="+userName,true)
    xhr1.send()
    xhr1.onload = function () {
        searchInput.innerHTML="<div><input id=\"name\" placeholder=\"按菜品名搜索\">\n" +
            "    <input id=\"price\" placeholder=\"按价格搜索\">\n" +
            "    类型：<select id=\"type\" >\n" +
            "        <option></option>\n" +
            "        <option>早餐</option>\n" +
            "        <option>中餐</option>\n" +
            "        <option>午餐</option>\n" +
            "        <option>甜食</option>\n" +
            "        <option>米</option>\n" +
            "        <option>面</option>\n" +
            "    </select>" +
            "<button onclick='search()'>搜索</button>\n"+
            "</div>"
        show.innerHTML=''
        show.innerHTML+='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">菜品名</th>\n' +
            '        <th style="border: solid;">种类</th>\n' +
            '        <th style="border: solid;">价格</th>\n' +
            '      </tr>'
        for (var i = 0; i < JSON.parse(xhr1.responseText).length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr1.responseText)[i].name + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr1.responseText)[i].type + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr1.responseText)[i].price + "元</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr1.responseText)[i].id +"' onclick='revise(this)'>修改</button>"+ "</td>" +
                "<td  style=\"border: solid;\">" +" <button id='"+ JSON.parse(xhr1.responseText)[i].id+"'   onclick='del(this)'>下架</button>"+ "</td>" +
                "</tr>"
        }
    }
}

function searchBill(){
    let shopUserName=document.getElementById("userName").innerText
    let userName=document.getElementById("user-name").value
    let name=document.getElementById("bill-name").value
    let price=document.getElementById("bill-price").value
    let type=document.getElementById("bill-type").value
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/showAllBill?shopUserName="+shopUserName+"&userName="+userName+"&name="+name+"&p="+price +"&type="+type,true)
    xhr.send()
    xhr.onload = function () {
        show.innerHTML=''
        show.innerHTML+="<tr>\n" +
            "    <th style=\"border: solid;\">用户名</th>\n" +
            "    <th style=\"border: solid;\">菜名</th>\n" +
            "    <th style=\"border: solid;\">价格</th>\n" +
            "    <th style=\"border: solid;\">类型</th>\n" +
            "</tr>"
        let v=JSON.parse(xhr.responseText)
        for (let i=0;i<v.length;i++){
            show.innerHTML+="<tr><td style=\"border: solid;\">"+ v[i].userName +"</td>"+
                "<td style=\"border: solid;\">"+ v[i].name +"</td>"+
                "<td style=\"border: solid;\">"+ v[i].price +"</td>"+
                "<td style=\"border: solid;\">"+ v[i].type +"</td>"+
                "</tr>"
        }
    }
}